package com.jfs.qy.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.jfs.qy.config.component.AlarmEvent;
import com.jfs.qy.config.component.AlarmEventBlockQueue;
import com.jfs.qy.constant.GlobalConstant;
import com.jfs.qy.entity.response.AlarmList;
import com.jfs.qy.entity.response.ChannelAlarm;
import com.jfs.qy.entity.response.ChannelInfo;
import com.jfs.qy.enums.AlertEventEnum;
import com.jfs.qy.session.NvrSessionStorage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Description 通道告警解析策略
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/4
 */
@Component
@AllArgsConstructor
@Slf4j
public class ChannelAlarmProcessStrategy implements AlarmEventProcessStrategy {


    @Override
    public boolean isMatch(List<AlarmList> alarmList) {
        if (CollectionUtil.isNotEmpty(alarmList)) {
            for (AlarmList alarmList1 : alarmList
            ) {
                if (CollectionUtil.isNotEmpty(alarmList1.getChannelAlarm())) {
                    return true;
                }
            }
        } else {
            return false;
        }
        return false;
    }

    @Override
    public void doProcessAlarmEvent(List<AlarmList> alarmList) {
        if (log.isDebugEnabled()){
            log.debug("通道告警解析策略内容:{}", JSONUtil.toJsonStr(alarmList));
        }
        for (AlarmList alarmList1 : alarmList
        ) {
            if (Objects.nonNull(alarmList1)&&CollectionUtil.isNotEmpty(alarmList1.getChannelAlarm())) {
                for (ChannelAlarm channelAlarm : alarmList1.getChannelAlarm()
                ) {
                    if (Objects.nonNull(channelAlarm.getIntAlarm()) /*&& channelAlarm.getInt_alarm().getAlarmVal()*/) {
                        //转换告警时
                        DateTime dateTime = DateUtil.parse(alarmList1.getTime(), GlobalConstant.NVR_ALARM_TIME_FORMAT);
                        String time = DateUtil.format(dateTime, DatePattern.NORM_DATETIME_PATTERN);
                        //封装统一事件对象
                        AlertEventEnum eventEnum = AlertEventEnum.getEnumByName(channelAlarm.getIntAlarm().getIntSubtype());
                        if (Objects.nonNull(eventEnum)){
                            AlarmEvent.Builder builder = new AlarmEvent.Builder()
                                    .alertEventEnum(eventEnum)
                                    .channelName(channelAlarm.getChannel())
                                    .type(eventEnum.getCode())
                                    .time(time);
                            if (StrUtil.isNotEmpty(channelAlarm.getIntAlarm().getAlogName())){
                                builder.name(channelAlarm.getIntAlarm().getAlogName());
                            }else {
                                builder.name(eventEnum.getDescribe());
                            }
                            ChannelInfo channel = NvrSessionStorage.getChannel(channelAlarm.getChannel());
                            if (Objects.nonNull(channel)){
                                builder.ip(channel.getIpAddress());
                            }
                            AlarmEvent alarmEvent = builder.build();
                            //放入队列中
                            AlarmEventBlockQueue.put(alarmEvent);
                        }

                    }
                }

            }
        }
    }
}
